<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct()
    {
        $this -> middleware('login');
    }
    public function index(Request $request)
    {
        $num = $request -> input('num',10);
        $keywords = $request -> input('keywords','');
        $data = DB::table('article as a1')
              -> select('a1.name as pname','a2.*',DB::raw("concat(a1.path,',',a1.id) as sortpath"))
              -> leftjoin('article as a2','a1.id','=','a2.pid')
              -> where([['a1.pid','=',0],['a2.name','like','%'.$keywords.'%']])
              -> orderBy('sortpath')
              -> paginate($num);
        return view('admin.article.index',['title'=>'文章管理','data' => $data,'request' => $request -> all()]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
           $data = DB::table('article as a1')
                 -> select('a1.*',DB::raw("concat( a1.path,',',a1.id ) as sortpath "))
                 -> leftjoin('article as a2','a1.pid','=','a2.id')
                 -> orderBy('sortpath')
                 -> get();
           foreach($data as  $row)
           {
                $num = substr_count($row -> path, ',');
                $str = str_repeat('|----',$num);
                $row -> name = $str.$row -> name;
           }
           return view('admin.article.add',['title' => '文章添加','data' => $data]);
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request -> except('_token','detail');
        if($data['pid'] == 0 )
        {
              $data['pid'] = 0;
              $data['path'] = 0;
        }else
        {
             $path = DB::table('article') -> where('id',$data['pid']) -> first() -> path;
             $data['path'] = $path.','.$data['pid'];
        }
        $time = Date('Y:m:d H:i:s');
        $data['created_at'] = $time;
        $data['updated_at'] = $time;
        if(empty($request -> detail))
        {
            $infoData['article_detail'] = '';
        }
        $infoData['article_detail'] = $request -> detail;
        $infoData['created_at'] = $time;
        $infoData['updated_at'] = $time;
        DB::beginTransaction();
        $res = DB::table('article') -> insertgetId($data);
        if($res > 0 )
        {
             $infoData['pid'] = $res;
             DB::table('article_info') -> insert($infoData);
             DB::commit();
             return redirect('/admin/article') -> with(['info' => '添加成功']);
        }else
        {
              DB::rollBack();
            return back() -> with(['info' => '添加失败']);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {

          $data = DB::table('article as a1')
                 -> select('a1.*',DB::raw("concat( a1.path,',',a1.id ) as sortpath "))
                 -> leftjoin('article as a2','a1.pid','=','a2.id')
                 -> orderBy('sortpath')
                 -> get();
          foreach ($data as $row) {
               $num = substr_count($row -> path,',');
               $str = str_repeat('|----',$num);
               $row -> name = $str.$row -> name;
          }
          $cateData = DB::table('article') -> where('id',$id) -> first();
          $res = DB::table('article_info') -> where('pid',$cateData -> id) -> first();
          if($res)
          {
               $cateData -> detail = $res -> article_detail;
          }else
          {
               $cateData -> detail = '';
          }

          // dd($cateData);
          if($data)
          {
                return view('admin.article.edit',['title' => '文章类型编辑','data' => $data,'cateData' => $cateData]);
          }else
          {
                return back() -> with(['info' => '不能修改']);
          }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
          $data = $request -> only('pid','name');
          if($data['pid'] == 0)
          {
              $data['path'] = 0;
          }else
          {
              $pPath = DB::table('article') -> where('id',$data['pid']) -> first() -> path;
              $data['path'] = $pPath.','.$data['pid'];
          }
          $time = Date('Y:m:d H:i:s');
          $data['created_at'] = $time;
          $data['updated_at'] = $time;
          $infoData['article_detail'] = $request -> detail;
          $infoData['pid'] = $id;
          $infoData['created_at'] = $time;
          $infoData['updated_at'] = $time;
          DB::beginTransaction();
          $res = DB::table('article') -> where('id',$id) -> update($data);
          if($res > 0)
          {
                 DB::table('article_info') -> where('pid',$id) -> update($infoData);
                 DB::commit();
                 return redirect('admin/article') -> with(['info' => '更新成功']);
          }else
          {
                 DB::rollBack();
                 return back() -> with(['info' => '更新失败']);
          }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
         $res = DB::table('article') -> where('pid',$id) -> first();
         if(!empty($res))
         {
               return back() -> with(['info' => '不能删除,有子分类']);
         }
         DB::beginTransaction();
         $res = DB::table('article') -> where('id',$id) -> delete();
         $result  = DB::table('article_info')-> where('pid',$id) -> delete();
         if($res && $result)
         {
              DB::commit();
              return redirect('admin/article') -> with(['info' => '删除成功']);
         }else
         {
              DB::rollBack();
              return back() -> with(['info' => '删除失败']);
         }


    }
}
